package com.wefans.wefansapp.activity;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;

import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.finals.FEATURES;
import com.wefans.wefansapp.common.tool.popup.DailyDialog;
import com.wefans.wefansapp.common.tool.popup.DailyDialog.OnDailyListener;
import com.wefans.wefansapp.common.tool.preference.MFSharedPreference;

/**
 * �묓떚鍮꾪떚��遺�え �대옒��
 * 
 * <pre>
 * [Drawer Menu �ъ슜 李멸퀬]
 * 1. activity_main.xml
 * 2. MainActivity.java
 * </pre>
 * 
 * @author kimhyeongbae
 */
public class BaseActivity extends Activity implements OnClickListener
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // final/static property
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // normal property
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private long           m_lPreviousButtonClickTime = 0;     // 踰꾪듉 �숈떆 �대┃ 諛⑹� 蹂�닔 �좎뼵
	protected SlidingDrawer m_sdQuickMenu             = null;
	
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // normal method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * Drawer �덉씠�꾩썐��異붽��쒕떎.
	 * 
	 * @return Drawer Menu Layout ��inflate ��Sub View 瑜�諛섑솚
	 */
	protected void addDrawerLayout(FrameLayout container)
	{
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout drawView = (RelativeLayout)layoutInflater.inflate(R.layout.layout_drawer_menu, null);
        container.addView(drawView);
        
        (drawView.findViewById(R.id.FL_DRAWER_1TH_MYINFO_PHOTO)).setOnClickListener(this);
        (drawView.findViewById(R.id.IB_DRAWER_MY_INFO_BTN)).setOnClickListener(this);
        (drawView.findViewById(R.id.LL_DRAWER_1TH_MY_STAR)).setOnClickListener(this);
        (drawView.findViewById(R.id.IV_DRAWER_2TH_MY_STARS)).setOnClickListener(this);
        (drawView.findViewById(R.id.IV_DRAWER_2TH_MY_TICKETS)).setOnClickListener(this);
        (drawView.findViewById(R.id.IV_DRAWER_2TH_CONTINUOUS_ATTENDANCE)).setOnClickListener(this);
        (drawView.findViewById(R.id.IV_DRAWER_2TH_MY_WINNING_LIST)).setOnClickListener(this);
        (drawView.findViewById(R.id.IV_DRAWER_2TH_ABOUT_MAMA)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_VOTE_NOW_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_BENEFITS_NOW_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_WINNER_NOTI_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_FREE_STAR_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_GONGJI_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_FAQ_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_CONTACT_US_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_SETTING_LABEL)).setOnClickListener(this);
        (drawView.findViewById(R.id.RL_DRAWER_3TH_VERSION_LABEL)).setOnClickListener(this);
	}
	
	/**
	 * 而⑦뀗痢��덉씠�꾩썐��異붽��쒕떎.
	 * 
	 */
	protected void addContentsLayout(FrameLayout container, int layoutResouceId)
	{
	    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    container.addView(layoutInflater.inflate(layoutResouceId, null));
	}
	
	/**
	 * 퀵메뉴를 화면에 추가한다.
	 * 
	 * @param container
	 */
	protected void addQuickMenuLayout(FrameLayout container)
	{
	    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    FrameLayout quickMenuLayout = (FrameLayout)layoutInflater.inflate(R.layout.layout_quick_menu, null);
        container.addView(quickMenuLayout);
        
        m_sdQuickMenu = (SlidingDrawer) quickMenuLayout.findViewById(R.id.SD_COMMON_QUICK_MENU);
        
        ((ImageButton) quickMenuLayout.findViewById(R.id.IV_COMMON_QUICKMENU_CONTENT_01)).setOnClickListener(this);
        ((ImageButton) quickMenuLayout.findViewById(R.id.IV_COMMON_QUICKMENU_CONTENT_02)).setOnClickListener(this);
        ((ImageButton) quickMenuLayout.findViewById(R.id.IV_COMMON_QUICKMENU_CONTENT_03)).setOnClickListener(this);
	}
	
	/**
	 * �대┃ �대깽��媛�뒫 �щ� �먮떒????
	 * 
	 * @return
	 */
	protected boolean isEnableClickEvent()
    {
        MFDebug.d(">> STPage::isEnableClickEvent()");
       
        if( System.currentTimeMillis() - m_lPreviousButtonClickTime <= 1000) {
            MFDebug.d("-- return (false)");
            return false;
        }
        
        m_lPreviousButtonClickTime = System.currentTimeMillis();
        MFDebug.d("-- return (true)");
        return true;
    }
	
	/**
	 * 베이스 액티비티를 상속받은 클래스에게 드로우어 메뉴를 클로우즈 할 수 있도록 노티를 주기 위한 메쏘드
	 * 
	 */
	protected void notifyDoClaseDrawerMenu() {}
	
	private boolean isTopActivity(String activityClassNameWithPackageName)
	{
	    boolean bTopActivity = false;
	    
	    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<RunningTaskInfo> info;
        info = activityManager.getRunningTasks(7);
        
        for(Iterator<RunningTaskInfo> iterator = info.iterator(); iterator.hasNext();) {
            RunningTaskInfo runningTaskInfo = (RunningTaskInfo) iterator.next();
            String topActivityClassName = runningTaskInfo.topActivity.getClassName();
            if(activityClassNameWithPackageName.equals(topActivityClassName)) {
                bTopActivity = true;
                break;
            }
        }
	    
	    return bTopActivity;
	}
	
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // interface methdo
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	@Override
	public void onClick(View v) 
	{
	    MFDebug.d(">> onClick()");
	    
	    if (!isEnableClickEvent())
            return;
	    
	    Intent intent = null;
	    
	    //Toast tsReady = Toast.makeText(getApplicationContext(), "준비중입니다.", Toast.LENGTH_SHORT);
	    
	    switch(v.getId()) {
	        case R.id.FL_DRAWER_1TH_MYINFO_PHOTO:
	    	case R.id.IB_DRAWER_MY_INFO_BTN:
	    	    if( isTopActivity("com.wefans.wefansapp.activity.MyinfoActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), MyinfoActivity.class);
                    startActivityForResult(intent,v.getId());
                }
	    		break;
	    	case R.id.LL_DRAWER_1TH_MY_STAR:
	    	    intent = new Intent(getApplicationContext(), MystarActivity.class);
				startActivityForResult(intent,v.getId());
	    		break;
	    	case R.id.IV_DRAWER_2TH_MY_STARS:
	    	    if( isTopActivity("com.wefans.wefansapp.activity.MyPointStarActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), MyPointStarActivity.class);
                    startActivityForResult(intent,v.getId());
                }
	    		break;
	    	case R.id.IV_DRAWER_2TH_MY_TICKETS:
	    	    if( isTopActivity("com.wefans.wefansapp.activity.MyPointCouponActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), MyPointCouponActivity.class);
                    startActivityForResult(intent,v.getId());
                }
	    		break;
	    	case R.id.IV_DRAWER_2TH_CONTINUOUS_ATTENDANCE:
	    		showDailyDialog();
	    	    break;
	    	case R.id.IV_DRAWER_2TH_MY_WINNING_LIST:
	    	    if( isTopActivity("com.wefans.wefansapp.activity.MywinningActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), MywinningActivity.class);
                    startActivityForResult(intent,v.getId());
                }
	    		break;
    	    case R.id.IV_DRAWER_2TH_ABOUT_MAMA :
    	        intent = new Intent(Intent.ACTION_VIEW);
                Uri u = Uri.parse(FEATURES.LINK_URL_MAMA_IMAGE);
                intent.setData(u);
                startActivity(intent);
    	        break;
    	    case R.id.RL_DRAWER_3TH_VOTE_NOW_LABEL : 
                if( isTopActivity("com.wefans.wefansapp.activity.MainActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_BENEFITS_NOW_LABEL : 
                if( isTopActivity("com.wefans.wefansapp.activity.BenefitActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), BenefitActivity.class);
                    startActivityForResult(intent,v.getId());
                }
				break;
    	    case R.id.RL_DRAWER_3TH_WINNER_NOTI_LABEL :
                if( isTopActivity("com.wefans.wefansapp.activity.BenefitResultActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), BenefitResultActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_FREE_STAR_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.SaveStarActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), SaveStarActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_GONGJI_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.NotificationActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), NotificationActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_FAQ_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.FAQActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
        	        intent = new Intent(getApplicationContext(), FAQActivity.class);
    				startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_CONTACT_US_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.ManToManActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), ManToManActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_SETTING_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.SettingsActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), SettingsActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	    case R.id.RL_DRAWER_3TH_VERSION_LABEL :
    	        if( isTopActivity("com.wefans.wefansapp.activity.VersionInfoActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), VersionInfoActivity.class);
                    startActivityForResult(intent,v.getId());
                }
    	        break;
    	        
    	    /** QUICK MENU LAYOUT **/
    	    case R.id.IV_COMMON_QUICKMENU_CONTENT_01 :
    	        if( isTopActivity("com.wefans.wefansapp.activity.MainActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                }
    	        break;
            case R.id.IV_COMMON_QUICKMENU_CONTENT_02 :
                if( isTopActivity("com.wefans.wefansapp.activity.BenefitActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), BenefitActivity.class);
                    startActivityForResult(intent,v.getId());
                }
                break;
            case R.id.IV_COMMON_QUICKMENU_CONTENT_03 :
                if( isTopActivity("com.wefans.wefansapp.activity.SaveStarActivity") ) {
                    notifyDoClaseDrawerMenu();
                }
                else {
                    intent = new Intent(getApplicationContext(), SaveStarActivity.class);
                    startActivityForResult(intent,v.getId());
                }
                break;
	    }
	}
	
	private void showDailyDialog()
    {
        MFDebug.d(">> showDailyDialog()");
        
    	final MFSharedPreference prf = new MFSharedPreference(getApplicationContext());
    	
		new DailyDialog(this, new OnDailyListener() {
			
			@Override
			public void onCheckDaily(boolean bSuccess) {
				if (bSuccess) {
					prf.setShowDaily();
				}
			}
		}).show();
    }
}
